package com.nari.ies.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.nari.ies.server.constant.Constant;
import com.nari.ies.server.entity.AreaAndPlantDO;
import com.nari.ies.server.entity.PlantType;
import com.nari.ies.server.service.IAreaAndPlantService;
import com.nari.ies.server.service.ICurveService;
import com.nari.ies.server.utils.DateUtil;
import com.nari.ies.server.utils.GeneraUtil;
import com.nari.ies.server.utils.XAxisUtil;
import com.nari.newGeneration.tableop.ITableOpNewGen;
import com.nari.newGeneration.tableop.impl.TableOpImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/AreaAndPlant")
public class AreaAndPlantController {

    @Autowired
    private IAreaAndPlantService areaAndPlantService;
    @Autowired
    private ICurveService curveService;

    static ITableOpNewGen tableNet = new TableOpImpl();
    /**
     * 查询区域名称和场站名称
     *
     * @param
     */
    @PostMapping("/getNeAreaInfo")
    public List<AreaAndPlantDO> getNeAreaInfo() {

        List<Map<String, Object>> maps = areaAndPlantService.queryNeAreaNameAndPlantName();
        List<AreaAndPlantDO> resultList = areaAndPlantService.createTreeDataStructure(maps);

        return resultList;
    }

    @PostMapping("/getPlantInfo")
    public List<Map<String, Object>> getPlantInfo(@RequestBody String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String type = jsonObject.getString("type");
        String bdzId = jsonObject.getString("bdzId");
        String areaId = jsonObject.getString("areaId");
        List<Map<String, Object>> maps =null;
        if(bdzId==null){
            maps = areaAndPlantService.queryPlantInfoByWFPId(type,areaId);
            log.info("场站集合"+maps);
        }else {
            maps = areaAndPlantService.queryPlantInfoByTypeAndId(type,bdzId,areaId);
            log.info("场站集合"+maps);
        }
        List<Map<String, Object>> mapsList = new ArrayList<>();

        // 有功、无功、可用出力、装机容量
        Integer[] columnIds =null;
        if(PlantType.isWind(type)){
            tableNet.open(1, 1, 1101000, 1, 223611);
            columnIds = new Integer[]{Constant.WINDPLANT_PGEN.getValue(), Constant.WINDPLANT_QGEN.getValue(),
                    Constant.WINDPLANT_AVAIL_POWER.getValue(), Constant.WINDPLANT_AND_PVPLANT_CAPACITY.getValue()};
        }else {
            tableNet.open(1, 1, 1101000, 1, 223621);
            columnIds =new Integer[]{Constant.PVPLANT_PGEN.getValue(), Constant.PVPLANT_QGEN.getValue(),
                    Constant.PVPLANT_AVAIL_POWER.getValue(), Constant.WINDPLANT_AND_PVPLANT_CAPACITY.getValue()};
        }

        for (Map<String, Object> map : maps) {
            String plantId = String.valueOf(map.get("ID"));
            String plantName = String.valueOf(map.get("NAME"));
            //String yc_id=plantId+"_0041";
            //Object[] obj = tableNet.tableGetByKey(plantId, columnIds);
            //log.info("实时库查询结果"+Arrays.toString(obj));

            /*===========测试数据开始=========*/
            Object[] obj  = new Object[]{10, 8, 10, 8};
            plantId="22362132000000000255";
            String yc_id="22362132000000000257";
            /*===========测试数据结束=========*/

            Map<String, Object> plantMap = new HashMap<>();
            plantMap.put("id",plantId);
            plantMap.put("name",plantName);
            plantMap.put("p",obj[0]);
            plantMap.put("q",obj[1]);
            plantMap.put("avail_power",obj[2]);
            plantMap.put("capacity",obj[3]);

            //获取当前时间
            Date nowDate = new Date();
            log.info("当前时间为"+nowDate);
            Date endDate = DateUtil.getNextDay(nowDate);
            log.info("结束时间为"+endDate);
            Date startDate = DateUtil.getToday();
            log.info("当天开始时间为"+startDate);
            List<Object> xAxisData = new ArrayList<Object>();

//-----------------------------------超短期预测-------------------------------------------
            //前半段，当天0点到当前时间
            List<Object> cdqycDataFirst = curveService.getCDQYCData(plantId, startDate, nowDate);
            log.info("前半段查詢超短期预测数据："+cdqycDataFirst);

            int firstNum=(int) ((nowDate.getTime() - startDate.getTime()) / (15*60 * 1000));
            Object[] firstOfCDQYCData = new Object[firstNum*3];
            if(cdqycDataFirst==null||cdqycDataFirst.size()==0){
                for (int i = 0; i < firstOfCDQYCData.length; i++) {
                    firstOfCDQYCData[i]=null;
                }
            }else {
                firstOfCDQYCData = GeneraUtil.getMiddleDataByFiveMiniutesWithList(cdqycDataFirst);
            }
            log.info("前半段超短期数据长度为："+firstOfCDQYCData.length);
            log.info("前半段超短期预测数据："+Arrays.toString(firstOfCDQYCData));
            //后半段，当前时间最近上传文件的后四个小时的数据
            List<LinkedHashMap<String,Object>> cdqycDataSecond =curveService.getCDQPoint5Data(plantId,nowDate);
            log.info("V0000-V0345结果集为："+cdqycDataSecond);

            ArrayList<Object> cdqycSecondlist = new ArrayList<>();
            if(cdqycDataSecond!=null&&cdqycDataSecond.size()>0){
                cdqycSecondlist = GeneraUtil.getMapToObj(cdqycSecondlist, cdqycDataSecond);
            }
            //前后拼接
            Object[] DataCDQYC = GeneraUtil.getCDQYCData(firstOfCDQYCData, cdqycSecondlist);
            log.info("超短期预测结果集:"+ Arrays.toString(DataCDQYC));

//----------------------------------短期预测---------------------------------------------
            List<LinkedHashMap<String,Object>> dqycMapList= curveService.getDQPoint5Data(plantId,startDate);
            //短期预测结果集处理
            Object[] DataDQYC = GeneraUtil.getDQYCData(dqycMapList);
            log.info("短期预测结果集:"+ Arrays.toString(DataDQYC));

//-----------------------------------总有功--------------------------------------
            Object[] DataZYG = curveService.getYcHsCueveData(yc_id, startDate, endDate, 5);
            log.info("总有功数据长度"+DataZYG.length);
            log.info("总有功数据"+Arrays.toString(DataZYG));

            xAxisData = XAxisUtil.getXAxisData(startDate, endDate, 96*3);
            plantMap.put("xAxis", xAxisData);
            plantMap.put("ZYGData", DataZYG);
            plantMap.put("CDQYCData", DataCDQYC);
            plantMap.put("DQYCData", DataDQYC);
            mapsList.add(plantMap);
        }
        return mapsList;
    }

}
